package com.example.module_attendance


import android.os.Build
import android.os.Handler
import android.util.Log
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.module_attendance.databinding.ActivityAttendanceBinding
import com.yuwei.base.BaseActivity
import com.yuwei.common.path.RouterPath
import com.yuwei.http.entity.SendAttendancesBody
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.math.log

@AndroidEntryPoint
@Route(path = RouterPath.ATTENDANCE)
class AttendanceActivity : BaseActivity<ActivityAttendanceBinding,AttendanceViewModel>() {
    val formatter = System.currentTimeMillis()
    val formatterTime1 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    val formatterTime = SimpleDateFormat("yyyy-MM-dd")
    val formatterTimeClass = SimpleDateFormat("EEEE")
    private lateinit var handler: Handler
    private lateinit var runnable: Runnable
    val currentTime = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
    var isCheck = false
    override fun initView() {
        binding.timeYear.setText(""+formatterTime.format(formatter)+ formatterTimeClass.format(formatter))
        handler = Handler()
        runnable = object :Runnable{
            override fun run() {
                val formatter1 = System.currentTimeMillis()
                val tt = currentTime.format(formatter1)
                binding.timeHour.setText(""+tt)
                if (tt.toString()<"12:00"){
                    binding.attendanceText.setText("上午打卡")
                    binding.attendanceTime.setText("9:00-12:00")
                }else{
                    binding.attendanceTime.setText("下午打卡")
                    binding.attendanceTime.setText("13:00-18:00")
                }
                handler.postDelayed(this,1000)
            }
        }
        handler.post(runnable)
        spTime()
        binding.attendanceDk.setOnClickListener {
            if (isCheck){
                var format = formatterTime1.format(formatter).toString()
                binding.attendanceDk.isEnabled = false
                val day = format.substring(8, 10)
                SPUtils.getInstance().put("time",day)
                spTime()
                SPUtils.getInstance().put("timeEnd",format)
                attendanceItem()
            }else{
                var format1 = formatterTime1.format(formatter).toString()
                isCheck = true
                SPUtils.getInstance().put("timeStart",format1)
            }
        }




        binding.history.setOnClickListener {
            ARouter.getInstance().build(RouterPath.MINUTES).navigation()
        }
        binding.attendanceBack.setOnClickListener {
            finish()
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacks(runnable)
    }

    override fun initData() {
        vm.getAttendancesSuccess.observe(this){
            ToastUtils.showLong("打卡成功")
        }
    }
    fun attendanceItem(){
        val timeEnd = SPUtils.getInstance().getString("timeEnd")
        val timeStart = SPUtils.getInstance().getString("timeStart")
        if (timeStart<"9:00"){
            val sendAttendancesBody = SendAttendancesBody(userId = 1, afternoonCheckIn = timeEnd, currentLocation = "lay,lon", id = 0, location = "C座13栋", morningCheckIn = timeStart, status = 1, user = 1)
            vm.getAttendances(sendAttendancesBody)
        }else if (timeStart>"9:00"){
            val sendAttendancesBody = SendAttendancesBody(userId = 1, afternoonCheckIn = timeEnd, currentLocation = "lay,lon", id = 0, location = "C座13栋", morningCheckIn = timeStart, status = 2, user = 1)
            vm.getAttendances(sendAttendancesBody)
        }else if (timeEnd<"18:00"){
            val sendAttendancesBody = SendAttendancesBody(userId = 1, afternoonCheckIn = timeEnd, currentLocation = "lay,lon", id = 0, location = "C座13栋", morningCheckIn = timeStart, status = 3, user = 1)
            vm.getAttendances(sendAttendancesBody)
        }else{
            val sendAttendancesBody = SendAttendancesBody(userId = 1, afternoonCheckIn = timeEnd, currentLocation = "lay,lon", id = 0, location = "C座13栋", morningCheckIn = timeStart, status = 4, user = 1)
            vm.getAttendances(sendAttendancesBody)
        }
    }
    fun spTime(){

        val int = SPUtils.getInstance().getString("time")
        val format1 = formatterTime1.format(formatter)
        val day = format1.substring(8, 10)
        binding.attendanceDk.isEnabled = !int.equals(day)
    }
}